#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void ParseMessage(const char *message, char *filename, char *http)
{
	int i=5;
	while ((message[i])!='\n')
	{		
		filename[i-5]=message[i];		
		i++;
	}

	for(i=0; i<9; i++)
		http[8-i]=filename[strlen(filename)-i-1];
	
	http[8] = '\0';
	filename[strlen(filename)-strlen(http)-2]='\0';
}

int FileExists(const char *fileName)
{
  FILE *fp;

	  
  if((fp=fopen(fileName, "rb"))==NULL)
  {
	//  printf("Me dio null\n");
    return 0;
  }
  else
  {  
  	fclose(fp);
    return 1;
  }  
}

long GetFileSize(FILE *fileDescr)
{
	long start, end;

	fseek(fileDescr, 0, SEEK_END);
	end = ftell(fileDescr);

	fseek(fileDescr, 0, SEEK_SET);
	start = ftell(fileDescr);
	
	return end-start;
}

void HttpResponse(char* message, const char* http, const char* filename, int success)
{
	switch(success)
	{
		case 0:
			strcpy(message, http);
			strcat(message, " 404 Not Found \n\n <b>ERROR</b>: File ");
			strcat(message, filename);
			strcat(message," was not found\n");
			break;
		case 1:
			strcpy(message, http);
			strcat(message, " 200 OK\n");
			strcat(message, "Content-type: text/html\n");
			strcat(message, "Content-Disposition: attachment; filename=\"");
			strcat(message, filename);
			strcat(message,"\"\n");
			strcat(message,"Content-type: application/octet-stream\n\n");
			break;
	}	
}
